package com.doctor.utils.sys;


import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import java.lang.reflect.Field;

/**
 * 主要用于改变系统电池那个栏的颜色
 *
 * @author nieyunlong
 */
public class TitleBarColorUtils {

    /**
     * 要想 用这个 在父布局中 加入属性
     *
     * @param activity
     */
    public static void setTitleBarSystemColor(Activity activity, int color, int flag, int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 状态栏透明 需要在创建SystemBarTintManager 之前调用。
            setTranslucentStatus(activity, true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        // 使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintColor(color);
        if (flag == 0) { // 线性 布局
            settingMarginTopLinearLayout(activity, id);
        } else if (flag == 1) { // 相对布局

            settingMarginTopRelativeLayout(activity, id);
        }
    }

    /**
     * flag 0代表 父布局是线性布局 1代表是相对不局 设置颜色
     *
     * @param activity
     */
    public static void setTitleBarSystemBg(Activity activity, int flag, int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 状态栏透明 需要在创建SystemBarTintManager 之前调用。
            setTranslucentStatus(activity, true);
            SystemBarTintManager tintManager = new SystemBarTintManager(
                    activity);
            // 使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarAlpha(0.0f);
            if (flag == 0) { // 线性 布局
                settingMarginTopLinearLayout(activity, id);
            } else if (flag == 1) { // 相对布局

                settingMarginTopRelativeLayout(activity, id);
            }

        }

    }

    @TargetApi(19)
    private static void setTranslucentStatus(Activity activity, boolean on) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        //FLAG_TRANSLUCENT_STATUS
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 获取系统状态栏高度
     *
     * @param activity
     * @return
     */
    public static int getStastusBarHeight(Activity activity) {
        try {
            Class<?> class1 = Class.forName("com.android.internal.R$dimen");
            Object object = class1.newInstance();
            Field field = class1.getField("status_bar_height");
            int x = Integer.parseInt(field.get(object).toString());
            return activity.getResources().getDimensionPixelSize(x);

        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;
    }

    /**
     * 这个父布局是线性的 用这个
     *
     * @param activity
     * @param id
     */
    public static void settingMarginTopLinearLayout(Activity activity, int id) {
        int stastusBarHeight = getStastusBarHeight(activity);
        View rl = (View) activity.findViewById(id);
        LayoutParams params = (LayoutParams) rl.getLayoutParams();
        params.topMargin = stastusBarHeight;
        rl.setLayoutParams(params);

    }

    /**
     * 这个父布局是线性的 用这个
     *
     * @param activity
     */
    public static void settingMarginTopLinearLayoutfg(Activity activity, View view) {
        int stastusBarHeight = getStastusBarHeight(activity);
        if (view != null) {
            LayoutParams params = (LayoutParams) view.getLayoutParams();
            params.topMargin = stastusBarHeight;
            view.setLayoutParams(params);
        }


    }

    /**
     * 这个父布局是线性的 用这个
     *
     * @param activity
     * @param id
     */
    public static void settingMarginTopRelativeLayout(Activity activity, int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int stastusBarHeight = getStastusBarHeight(activity);
            View rl = (View) activity.findViewById(id);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl
                    .getLayoutParams();
            params.topMargin = stastusBarHeight;
            rl.setLayoutParams(params);
        }

    }
    public static void settingMarginTopRelativeLayout(Activity activity, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int stastusBarHeight = getStastusBarHeight(activity);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            params.topMargin = stastusBarHeight;
            view.setLayoutParams(params);
        }
    }
    /**
     * 背景 （只使用于一个图片的布局）
     */
    public static void setBg(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 状态栏透明 需要在创建SystemBarTintManager 之前调用。
            setTranslucentStatus(activity, true);
            SystemBarTintManager tintManager = new SystemBarTintManager(
                    activity);
            // 使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(Color.TRANSPARENT);
            tintManager.setStatusBarAlpha(0.0f);

        }
    }

    /**
     * 颜色
     *
     * @param activity
     * @param color
     */
    public static void setStatusColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 状态栏透明 需要在创建SystemBarTintManager 之前调用。
            setTranslucentStatus(activity, true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        // 使StatusBarTintView 和 actionbar的颜色保持一致，风格统一。
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintColor(color);
    }

    /**
     * 这个父布局是线性的 用这个
     *
     * @param activity
     * @param id
     */
    public static void settingMarginTopLinearLayoutfg(Activity activity, View view, int id) {
        int stastusBarHeight = getStastusBarHeight(activity);
        View rl = (View) view.findViewById(id);
        LayoutParams params = (LayoutParams) rl.getLayoutParams();
        params.topMargin = stastusBarHeight;
        rl.setLayoutParams(params);

    }

    /**
     * 模仿一个状态栏 外边是线性布局 调用这个
     */
    public static void customStatusLinearLayout(Activity activity, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int height = getStastusBarHeight(activity);
            if (view != null) {
                LayoutParams params = (LayoutParams) view.getLayoutParams();
                params.height = height;
                view.setLayoutParams(params);
            }

        } else {
            if (view != null) {
                view.setVisibility(View.GONE);
            }

        }


    }

    /**
     * 模仿一个状态栏 外边是相对布局 调用这个
     */
    public static void customStatusRelativeLayout(Activity activity, View view) {
        int height = getStastusBarHeight(activity);
        if (view != null) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
            params.height = height;
            view.setLayoutParams(params);
        }

    }

    public static void settingMarginTopLinearLayout(Activity activity, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (view != null) {
                int stastusBarHeight = getStastusBarHeight(activity);
                LayoutParams params = (LayoutParams) view.getLayoutParams();
                params.topMargin = stastusBarHeight;
                view.setLayoutParams(params);
            }

        }
    }
    public static void settingMarginTopRel(final Activity activity, final View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (view != null) {
                int stastusBarHeight = getStastusBarHeight(activity);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
                params.topMargin = stastusBarHeight;
                view.setLayoutParams(params);
            }

        }
    }
    public static void settingMarginTopGone() {
        /**
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         if (tintManager != null) {
         tintManager.setStatusBarTintEnabled(false);
         }
         }
         */

    }

    public static void settingMarginStatusVis() {
        /**
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
         if (tintManager != null) {
         tintManager.setStatusBarTintEnabled(true);
         }
         }
         */
    }


}
